package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreenPointLogic;
import cn.topevery.garden.entity.dto.GreenPointDTO;
import cn.topevery.garden.entity.query.GreenPointQuery;
import cn.topevery.garden.entity.query.PointQuery;
import cn.topevery.garden.entity.vo.GreenPointVO;
import cn.topevery.garden.entity.po.GreenPoint;
import cn.topevery.garden.entity.vo.PointVO;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 样点表
 *
 * @author code-generate
 * @date 2023-11-17
 */
@Api(tags = "样点表")
@RestController
@RequestMapping("/greenPoint")
public class GreenPointController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreenPointLogic greenPointLogic;


    /**
     * 获取样点表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取样点表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreenPointVO>> listPage(@RequestBody GreenPointQuery query) {
        return JsonResult.ok(greenPointLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 样点表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreenPointDTO dto) {
        greenPointLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, GreenPoint.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreenPointVO> getInfoById(@RequestParam Integer id) {
        GreenPoint entity = greenPointLogic.getEntity(id);
        GreenPointVO vo= mapper.map(entity, GreenPointVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greenPointLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出样点表信息")
    @GetMapping("/export")
    public void export(GreenPointQuery query, HttpServletResponse response) {
        List<GreenPointVO> list = greenPointLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "样点表", "sheet1", GreenPointVO.class, "样点表", response);
    }

    @ApiOperation(value = "获取样点管理表页列表")
    @PostMapping("/getBySectionId")
    public JsonResult<DataPager<GreenPointVO>> getBySectionId(@RequestBody GreenPointQuery query) {
        return JsonResult.ok(greenPointLogic.getBySectionId(query));
    }

}
